package hezi.helper;

import hezi.common.SystemConfig;
import hezi.core.ForumGroup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.List;

import org.apache.log4j.Logger;

import dz.common.DBHelper;
import dz.common.PropTool;

public class ForumGroupDBHelper {

	public synchronized static void creaetable(Statement stat) {
		try {
			if(PropTool.getBoolean("droptable")){
				stat.execute("drop table if exists forumgroup");
			}
			
			stat.executeUpdate("create table if not exists forumgroup (" +
					"groupid char(10)," +
					"groupname varchar(10)," +
					"priority integer(3)" +

					");");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void insert(ForumGroup group) {
		
		String sqlstr = "insert into forumgroup values(?,?,?)";
		
		try {
			PreparedStatement prep = conn.prepareStatement(sqlstr);
			prep.setString(1,group.groupid);
			prep.setString(2,group.groupname);
			prep.setInt(3,group.priority);
			
			prep.executeUpdate();
			prep.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public synchronized static void sets(ResultSet rs, ForumGroup group) throws SQLException {
		group.groupid = rs.getString("groupid");
		group.groupname = rs.getString("groupname");
		group.priority = rs.getInt("priority");
	}


	public static void select(List<ForumGroup> groups) {
		String sql =  "SELECT * FROM forumgroup ORDER BY priority DESC";
		ResultSet rs;
		try {
			rs = conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				ForumGroup group = new ForumGroup();
				sets(rs,group);
				groups.add(group);
			}
			rs.close();
			rs.getStatement().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static ForumGroup select(String groupid) {
		String sql =  "SELECT * FROM forumgroup WHERE groupid='" + groupid + "'";
		ResultSet rs;
		try {
			rs = conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				ForumGroup group = new ForumGroup();
				sets(rs,group);
				return group;
			}
			rs.close();
			rs.getStatement().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public static void select2(Collection<ForumGroup> groups) {
		String sql =  "SELECT * FROM forumgroup ORDER BY priority DESC";
		ResultSet rs;
		try {
			rs = conn.createStatement().executeQuery(sql);
			while (rs.next()) {
				String id = rs.getString("groupid");
				if(!id.contains("allforum") && ForumGroup.isContains(id)){
					groups.add(ForumGroup.getGroup(id));
				}
			}
			rs.close();
			rs.getStatement().close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	static Logger logger = Logger.getLogger("");
	static String dbfilepath = SystemConfig.getInstance().getSysDBPath() + "config.db";
	static Connection conn = DBHelper.getConnection(dbfilepath);
	static{
		try {
			creaetable(conn.createStatement());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void update(String forumid, String key, String newvalue) {
		String sql = "UPDATE forumgroup SET " + key + "='" + newvalue + "' WHERE forumid = '" + forumid + "'";
		try {
			Statement stat = conn.createStatement();
			stat.executeUpdate(sql);
			stat.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
